package com.example.administrator.myokhttpmvp.utils;

import android.content.Context;
import android.content.SharedPreferences;

import java.util.Set;

/**
 * 存储封装
 */
public class PrefManager {

    private static PrefManager sInstance;
    private final SharedPreferences mPref;

    private PrefManager(Context context, String perFrence) {
        mPref = context.getSharedPreferences(perFrence, Context.MODE_PRIVATE);
    }

    public static synchronized void initializeInstance(Context context, String perFrence) {
        if (sInstance == null) {
            sInstance = new PrefManager(context, perFrence);
        }
    }

    public static synchronized PrefManager getInstance() {
        if (sInstance == null) {
            throw new IllegalStateException(PrefManager.class.getSimpleName() +
                    " is not initialized, call initializeInstance(..) method first.");
        }
        return sInstance;
    }

    public SharedPreferences get() {
        return this.mPref;
    }

    public void setString(String key, String value) {
        mPref.edit().putString(key, value).commit();
    }

    public void setInt(String key, int value) {
        mPref.edit().putInt(key, value).commit();
    }

    public void setLong(String key, long value) {
        mPref.edit().putLong(key, value).commit();
    }

    public void setBoolean(String key, Boolean value) {
        mPref.edit().putBoolean(key, value).commit();
    }

    public void setSet(String key, Set<String> data) {
        mPref.edit().putStringSet(key, data).commit();
    }

    public void remove(String key) {
        mPref.edit().remove(key).commit();
    }

    public boolean clear() {
        return mPref.edit()
                .clear()
                .commit();
    }

}
